<template>
  <div>
    你的购物车有如下商品：

    <el-table :data="shopArray" style="width: 100%">
      <el-table-column prop="productBean.name" label="商品名" width="180"/>
      <el-table-column prop="productBean.price" label="单价" width="180"/>
      <el-table-column prop="productBean.addr" label="产地" width="180"/>
      <el-table-column prop="productBean.name" label="购买数量" width="180">
        <template #default="scope">
          <el-input v-model="scope.row.number" @blur="updateNum(scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="单项总价" width="180">
        <template #default="scope">
          {{ scope.row.number * scope.row.productBean.price }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button
              type="danger"
              @click="del(scope.row.id)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="$router.push('/shopIndex')">继续购物</el-button>
    <el-button type="primary" @click="$router.push('/order')">结账</el-button>

  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      shopArray: [],
    }
  },
  methods: {
    findShop() {
      axios.get("/project/cart/findAll").then(resp => {
        this.shopArray = resp.data;
      })
    },
    del(id) {
      axios.get("/project/cart/remove", {params: {cartId: id}}).then(resp => {
        if (resp.data == "ok") {
          this.findShop();
        }
      })
    },
    updateNum(shopObj) {
      axios.get("/project/cart/update", {
        params: {
          cartId: shopObj.id,
          newNum: shopObj.number
        }
      }).then(resp => {
        if (resp.data == "ok") {
          this.findShop();
        }
      })
    }
  },
  created() {
    this.findShop();
  }
}
</script>

<style scoped>

</style>